import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/user/LoginView.vue";
import PcLayout from '@/layout/pc/index.vue';
import { removeToken } from '@/utils/auth.js'; // 导入退出登录所需的方法


import Layout from "@/layout";
import index from "@/layout/index.vue";
import {getMenu} from '@/api/rbac_menu.js'
import {EventBus} from '@/api/listen.js'
import {getAccessToken} from '@/utils/auth.js'
import layout from "@/layout/index.vue";


Vue.use(VueRouter)

const routes = [//初始静态路由
  {
    path: '/',
    redirect: '/home',
    component: Layout, // 修改为 Layout 组件
    children: [
      {
        path: '/home',
        component: () => import( '../views/user/HomeView.vue'),
        name: 'home'
      },
      {
        path: '/dishes',
        component: () => import( '../views/user/Dishes.vue'),
        name: 'dishes'
      },
      {
        path: '/cafeteria-events',
        component: () => import( '../views/user/Events.vue'),
        name: 'CafeteriaEvents'
      },
      {
        path: '/cafeteria-traffic',
        component: () => import( '../views/user/CafeteriaTraffic.vue'),
        name: 'CafeteriaTraffic'
      },
    ]
  },
  // {
  //   path: '/home',
  //   component: PcLayout,
  //   children: [
  //     {
  //       path: '',
  //       component: () => import('../views/HomeView.vue'),
  //       name: 'home'
  //     }
  //   ]
  // },

  {
    path: '/login',
    name: 'login',
    component:  LoginView
  },
]

let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}

const  router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

async function fetchMenu() {
  getMenu().then((res) => {
    const menuList = res.data;
    console.log('menuList',menuList);

    menuList.forEach(menu => {

      const route = {
        path: menu.path,
        // name: menu.name,
        component: Layout,
        children:[{
          path: '',
          component: () => import(`@/views/${menu.component}.vue`),
      }]
      };
      router.addRoute(route);
    });
  }).catch((error) => {
    console.error(error);
  });
}
//
// async function fetchMenu() {
//   try {
//     const response = await getMenu();
//     const menuList = response.data;
//     console.log('menuList', menuList);
//
//     menuList.forEach(menu => {
//       const route = {
//         path: menu.path,
//         component: Layout,
//         children:[{
//           path: '',
//           component: () => import(`@/views/${menu.component}.vue`),
//         }]
//       };
//       router.addRoute(route);
//     });
//   } catch (error) {
//     console.error(error);
//     handleFetchMenuError(); // 处理错误
//   }
// }
//
//
// function handleFetchMenuError() {
//   console.error('Error:', error);
//   // 检查错误类型，例如通过状态码或错误消息
//   if (error.response && error.response.status === 401) { // 假设 401 表示 token 过期
//     this.$alert('您的登录已过期，请重新登录。', '登录过期', {
//       confirmButtonText: '确定',
//       callback: () => {
//         this.logout();
//         this.$router.push('/login');
//       }
//     });
//   } else {
//     // 处理其他类型的错误
//   }
// }
//
// function logout() {
//   // 这里放置您的登出逻辑，比如删除令牌、清除状态等
//   removeToken();  // 假设 removeToken 是您移除令牌的方法
//   this.isLoggedIn = false; // 更新登录状态
//   // 重置角色和菜单列表
//   this.role = ''; // 或设置为默认角色
//   this.menuList = []; // 清空菜单列表
//
//   this.$router.push('/home'); // 导航到主页
// }




if(getAccessToken()){
  fetchMenu();
}
// 监听事件
EventBus.$on('login-success', fetchMenu);
export default router
